package com.eddie.factory.factorymethod.pizzastore.order;

import com.eddie.factory.constant.PizzaConstant;
import com.eddie.factory.factorymethod.pizzastore.pizza.*;
import lombok.SneakyThrows;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * @author Eddie
 * @date 2022/4/29 11:24
 */
public abstract class OrderPizza {
    protected OrderPizza() {
        do {
            PizzaConstant type = getType();
            if (type == null) {
                break;
            }
            Pizza pizza = createPizza(type);
            if (pizza != null) {
                pizza.prepare();
                pizza.bake();
                pizza.cut();
                pizza.box();
            }

        } while (true);
    }


    /**
     * 创建不同口味的披萨（该方法提供给子类）
     *
     * @param type   披萨类型
     * @param region 地区
     */
    protected Pizza createTastePizza(PizzaConstant type, String region) {
        Pizza pizza = null;
        if (type == PizzaConstant.CHEESE_NAME) {
            pizza = new CheesePizza(region);
        } else if (type == PizzaConstant.GREEK_NAME) {
            pizza = new GreekPizza(region);
        } else if (type == PizzaConstant.PEPPER_NAME) {
            pizza = new PepperPizza(region);
        }
        return pizza;
    }

    /**
     * 创建地区披萨
     *
     * @param type
     * @return
     */
    public abstract Pizza createPizza(PizzaConstant type);

    @SneakyThrows
    private PizzaConstant getType() {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("input pizza type:");
        PizzaConstant result = null;
        System.out.println("1、芝士披萨");
        System.out.println("2、希腊披萨");
        System.out.println("3、胡椒披萨");
        String s = bufferedReader.readLine();
        if ("1".equals(s)) {
            result = PizzaConstant.CHEESE_NAME;
        } else if ("2".equals(s)) {
            result = PizzaConstant.GREEK_NAME;
        } else if ("3".equals(s)) {
            result = PizzaConstant.PEPPER_NAME;
        }
        return result;
    }

}
